<?php
/**
 * Created by PhpStorm.
 * User: north
 * Date: 2017/7/4
 * Time: 下午7:44
 */
$url = $_POST['url'];
$i =0;
while($i<= 5) {
    $test = file_get_contents('http://image.baidu.com/n/pc_search?queryImageUrl='.$url.'&fm=searchresult&pos=urlsearch&uptype=urlsearch');
    
    $test = htmlspecialchars($test);
    
    preg_match('/\'sameList\'(.+?)\'sameSizeNum\'/is',$test,$match);
    $tmp = str_replace(": Array ? ","",$match[1]);
    $tmp = str_replace("}],","}]",$tmp);
    $tmp = str_replace('] : [',',',$tmp);
    $tmp = str_replace('&quot;','"',$tmp);
    
    if (empty(json_decode($tmp))) {
        preg_match('/\'simiList\': (.+?)\'faceNum\'/is',$test,$match1);
        //$simiList = str_replace(":","",$match1[1]);

        //$simiList = ltrim($match1[1],':');

        $simiList = str_replace('&quot;','"',$match1[1]);

        $simiList = substr($simiList,0,-11);
        $simiList = ltrim($simiList,'[');

        if (empty($simiList) || $simiList=='nul') {
            if ($i > 5) {
                echo '{"data": null,"msg":null}';exit;
            } else {
                $i++;
            }

        } else {
            echo ('{"data":['.$simiList.'],"msg":"找不到相同的图"}');exit;
        }


    } else {

        echo '{"data":'.$tmp.',"msg":null}';exit;
    }
}

